package org.douxiaoban.client.activity;

import java.util.Timer;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.mobclick.android.MobclickAgent;
import org.douxiaoban.client.R;
import org.douxiaoban.client.app.Assistant;
import org.douxiaoban.client.util.ThreadTool;
import org.douxiaoban.client.widget.PopupRecommend;
import org.douxiaoban.client.widget.TitleStyle;

public class SendMailActivity extends BaseActivity{
	private Handler mHandler;
	private String mUserId;
	private String mUserName;
	
	private EditText mEditTitle;
	private EditText mEditContent;
	private EditText mEditCaptchaStr;
	private ImageView mImageToken;
	private View mCaptchaLayout;
	
	private String mCaptchaToken;
	private String mCaptchaStr;  //the str default_bg_image link;
	private boolean mHasCaptcha;
	private TitleStyle mTitle;
	
	private Button mBtnSend;
	
	private ProgressDialog mDialog;
	
	PopupRecommend mPopup;
	
	@Override
	public void onCreate(Bundle bundle){		
		super.onCreate(bundle);
		
		Intent intent = this.getIntent();
		mUserId = intent.getStringExtra(Assistant.KEY_USER_ID);
		mUserName = intent.getStringExtra(Assistant.KEY_USER_NAME);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.send_mail);
	    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
        mTitle = new TitleStyle(this);
        mTitle.initTitleStyle();
        mTitle.initPageController(1,1);
        mTitle.setTitle(getString(R.string.titleSendMail));
		
	    mEditTitle = (EditText)findViewById(R.id.sendMailTitle);
	    mEditContent = (EditText)findViewById(R.id.sendMailContent);
	    mEditCaptchaStr = (EditText)findViewById(R.id.sendMailEditToken);
	    mImageToken = (ImageView)findViewById(R.id.sendMailImageToken);
	    mEditCaptchaStr = (EditText)findViewById(R.id.sendMailEditToken);
	    mCaptchaLayout = findViewById(R.id.captchaLayout);
		mHasCaptcha = false;
		
		mPopup = new PopupRecommend(this);
		
		mHandler = new Handler(){
	     	@Override
	     	public void handleMessage(Message msg){
	     		mBtnSend.setClickable(true);
	     		Bundle bundle = msg.getData();
	     		switch(msg.what){
	     		case Assistant.MSG_SUCCESS:
	     			//toast success;
	     			setResult(Assistant.REP_SUCCESS);
	     			finish();
	     			break;
	     		case Assistant.MSG_NEED_CAPTCHA:
	     			mHasCaptcha = true;
	     			mCaptchaLayout.setVisibility(View.VISIBLE);
	     			mCaptchaToken = bundle.getString(Assistant.KEY_CAPTCHA_TOKEN);
	     			mCaptchaStr = bundle.getString(Assistant.KEY_CAPTCHA_URL);
	     			ThreadTool.instace(SendMailActivity.this).getImage(mCaptchaStr, this, -1);
	     			break;
	     		case Assistant.MSG_UN_SUCCESS:
	     			//toast unSuccess;
	     			mPopup.showToastWindow(Assistant.FAIL_POP, "豆邮发送失败");
	     			break;
	     		case Assistant.TOOLS_MSG_IMAGE:
	     			Bitmap bitmap = ((ThreadTool.ImageSerialz)
	     							bundle.getSerializable(Assistant.TOOLS_KEY_IMAGE)).m_bitmap;
	     			mImageToken.setImageBitmap(bitmap);
	     			break;
	     		}
	     		
	     	}
		};
		
		initView();
	}
	private void initView(){
		
		
		mBtnSend = (Button)findViewById(R.id.sendMailBtnSend);
	    Button btnCancel = (Button)findViewById(R.id.sendMailBtnCancel);

	    TextView senderName = (TextView)findViewById(R.id.sendMailSenderName);
	    senderName.setText(mUserName);
	    							
	    mBtnSend.setOnClickListener(new OnClickListener(){
	    	@Override
	    	public void onClick(View view){
	    		mBtnSend.setClickable(false);
	    		String title =  mEditTitle.getText().toString();
	    		String content = mEditContent.getText().toString();
	    		if(!mHasCaptcha){	    	    
	    			ThreadTool.instace(SendMailActivity.this).sendMail(mHandler,
		    														  mUserId,
		    														  title,
		    														  content);
	    		}else{
	    			String tokenStr = mEditCaptchaStr.getText().toString();
	    			if(0 == tokenStr.length()){
	    				//toast input the tokenStr
	    			}else{
	    				ThreadTool.instace(SendMailActivity.this).sendMail(mHandler,
																		   mUserId,
																		   title,
																		   content,
																		   mCaptchaToken,
																		   tokenStr);
	    			}
	    			
	    		}
	    	}
	    });
	    
	    btnCancel.setOnClickListener(new OnClickListener(){
	    	@Override
	    	public void onClick(View view){
	    		finish();
	    	}
	    });
	}
	
      @Override
    protected void onResume() {
        MobclickAgent.onResume(this);
        super.onResume();    //Super Method super.onResume();
    }

    @Override
    protected void onPause() {
        MobclickAgent.onPause(this);
        super.onPause();    //Super Method super.onPause();
    }
}



